package com.dong.c03_channel;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * <h1></h1>
 *
 * @author : dzj
 * @create: 2020-03-17 22:58
 */
public class P2_NioFileChannleRead {
    public static void main(String[] args) throws IOException {
        String fileName = "d:\\file01.txt";
        File file = new File(fileName);
        FileInputStream fis = new FileInputStream(file);
        FileChannel channel = fis.getChannel();
        ByteBuffer buffer = ByteBuffer.allocate(200);
        int len=0;
        byte[] buf = new byte[1024];
        while((len = channel.read(buffer))!=-1){

            channel.read(buffer);
            buffer.flip();
            buffer.get(buf,0,len);
      /*     int pos =0;
            while(buffer.hasRemaining()) {
                byte b = buffer.get();
                buf[pos++]=b;
            }*/
         //   System.out.println("***");
          //  System.out.println(new String(buf,0,buffer.limit()-1));
            System.out.println(new String(buffer.array()));
            buffer.flip();
        }
        channel.close();
        fis.close();

    }
}
